<?php
/**
 * Function Library for HTML Form Elements
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: formelements.inc.php 121 2010-08-20 09:21:19Z sruttloff@googlemail.com $
 * @link      phpmy
 */

/**
 * returns a well formed input tag
 *
 * @param array $array    array with attributes for input tag
 * @param bool  $required mark as needed (default: false)
 * @param bool  $mark     mark it anyway (default: false)
 *
 * @return string input tag
 */
function formInput($array = array (), $required = false, $mark = false)
{
    if (!is_array($array))
        return false;
    $origin_array = array (
        'NAME' => 'unnamed',
        'TYPE' => 'TEXT',
        'SIZE' => '20',
        'MAXLENGTH' => '255',
        'VALUE' => ''
    );
    // uppercase
    foreach ($array as $k => $v)
    {
        unset ($array[$k]);
        $array[strtoupper($k)] = $v;
    }
    $tag = "<input ";
    $result = array_merge($origin_array, $array);
    switch (strtoupper($result['TYPE']))
    {
        case "HIDDEN" :
            unset ($result['CLASS'], $result['SIZE'], $result['MAXLENGTH']);
            break;
        case "SUBMIT" :
            unset ($result['SIZE'], $result['MAXLENGTH']);
            if ($result['CLASS'] == "form")
            $result['CLASS'] = "formbutton";
            $result['OnClick'] = "_body_onunload(); this.form.submit();";
            $result['TYPE'] = "button";
            break;
        case "RADIO" :
            $result['CLASS'] = "formradio";
            break;
    }
    foreach ($result as $k => $v)
    {
        if ($v == "")
            continue;
        if (strtoupper($k) == "VALUE" AND !preg_match("=^(submit)$=si", $result["TYPE"]))
            $tag .= strtoupper($k) . '="' . HtmlFormat(stripslashes($v)) . '" ';
        else
            $tag .= strtoupper($k) . '="' . $v . '" ';
    }
    // if no value given try to autoset
    if (sendvar($result['NAME']) && $result['VALUE'] == "")
        $tag .= 'VALUE="' . HtmlFormat(stripslashes(sendvar($result['NAME']))) . '" ';
    if (($required && !sendvar($result['NAME'])) || $mark)
        $tag = REQUIREDGRAPH . $tag;
    return $tag . "/>";
}
/**
 * returns a well formed textarea tag
 *
 * @param array $array    array with attributes for textarea tag
 * @param bool  $required mark as needed (default: false)
 * @param bool  $mark     mark it anyway (default: false)
 *
 * @return string input tag
 */
function formTextarea($array = array (), $required = false, $mark = false)
{
    if (!is_array($array))
        return false;
    $origin_array = array (
        'NAME' => 'unnamed',
        'COLS' => '80',
        'ROWS' => '8'
    );
    // uppercase
    foreach ($array as $k => $v)
    {
        unset ($array[$k]);
        $array[strtoupper($k)] = $v;
    }
    $result = array_merge($origin_array, $array);
    // extract value and remove from entity
    if (isset ($result['VALUE']))
    {
        sendvar_set($result['NAME'], $result['VALUE']);
        unset ($result['VALUE']);
    }
    $tag = "<TEXTAREA ";
    foreach ($result as $k => $v)
    {
        if ($v == "")
            continue;
        $tag .= strtoupper($k) . '="' . $v . '" ';
    }
    if (($required && !sendvar($result['NAME'])) || $mark)
        $tag = REQUIREDGRAPH . $tag;
    $tag .= '>' . HtmlFormat(stripslashes(sendvar($result['NAME'])));
    return $tag . "</TEXTAREA>";
}
/**
 * returns a well formed select tag
 *
 * @param array $array     array with attributes for select tag
 * @param array $rows      assoc.array with values and text e.g. array("1" => "any text")
 * @param array $preselect flat array with values that be preselcted e.g. select value 1 = array("1")
 * @param bool  $mark      mark it anyway (default: false)
 *
 * @return string select tag
 */
function formSelect($array = array (), $rows = array (), $preselect = array (), $mark = false)
{
    if (!is_array($array)) return false;
    $origin_array = array ("NAME" => "unnamed", "SIZE" => "5");
    // uppercase
    foreach ($array as $k => $v)
    {
        unset ($array[$k]);
        $array[strtoupper($k)] = $v;
    }
    $a_preselect = array();
    if ($preselect)
    {
        // make array of preselect
        if (!is_array($preselect))
            $preselect = array ((string) $preselect);
        // cast preselct vals as array
        foreach ($preselect as $val)
        {
            $a_preselect[] = (string) $val;
        }
    }
    $result = array_merge($origin_array, $array);
    // check if multiple
    if (!isset ($result['MULTIPLE']) && !isset ($array['SIZE'])) unset ($result['SIZE']);
    $tag = '<select ';
    foreach ($result as $k => $v)
    {
        if ($v == "") continue;
        $tag .= strtolower($k) . '="' . $v . '" ';
    }
    $tag .= ">";
    // place values and text
    foreach ($rows as $k => $v)
    {
        $k = (string) $k;
        $tag .= '<option value="' . $k . '"';
        if (in_array($k, $a_preselect)) $tag .= ' selected="selected"';
        $tag .= '>' . ($v) . '</option>';
    }
    if ($mark) $tag = REQUIREDGRAPH . $tag;
    return $tag . "</select>";
}
/**
 * returns a well formed checkbox
 *
 * @param array $array    array with attributes for textarea tag
 * @param bool  $required mark as needed (default: false)
 * @param bool  $mark     mark it anyway (default: false)
 *
 * @return string input tag
 */
function formCheckbox($array = array (), $required = false, $mark = false)
{
    if (!is_array($array))
        return false;
    $origin_array = array (
        "CLASS" => "formcheck",
        "NAME" => "unnamed",
        "VALUE" => "",
        "TYPE" => "checkbox",
        "CHECKED" => ""
    );
    // uppercase
    foreach ($array as $k => $v)
    {
        unset ($array[$k]);
        $array[strtoupper($k)] = $v;
    }
    $tag = "<INPUT ";
    $result = array_merge($origin_array, $array);
    foreach ($result as $k => $v)
    {
        if ($v == "")
            continue;
        if (strtoupper($k) == "VALUE")
            $tag .= strtoupper($k) . '="' . htmlFormat(stripslashes($v)) . '" ';
        else
            $tag .= strtoupper($k) . '="' . $v . '" ';
    }
    // if no value given try to autoset
    if (sendvar($result['NAME']) && $result['VALUE'] == "")
        $tag .= 'checked="checked" ';
    if (($required && !sendvar($result['NAME'])) || $mark)
        $tag = REQUIREDGRAPH . $tag;
    return $tag . "/>";
}